'''用zip打包,不压缩只打包.需要用rar添加恢复记录的自己再rar吧'''

import os
import sys
import zipfile

class ZipBackup:
  
  ## 参数依次为:创建的压缩包,压缩包中的根目录名,后面是3个备份信息文件
  def __init__(self, 
    zipFpath = sys.path[0]+'\\ZipBackup.zip',
    zipRoot = 'Backup_zip',
    logFiles = sys.path[0]+'\\logFiles.txt', 
    logDirs = sys.path[0]+'\\logDirs.txt', 
    logInfo = sys.path[0]+'\\logInfo.txt'):
    
    # 复制参数
    self._zipFpath = zipFpath
    self._zipRoot = zipRoot
    self._logFiles = logFiles
    self._logDirs = logDirs
    self._logInfo = logInfo
    
    # 读取根目录,备份信息
    with open(self._logInfo, 'rb') as f:
      lines = f.read().decode('utf_8_sig').split('\n')
      self._rootDir = (lines[0].split('='))[1].strip()
      self._startTime = (lines[1].split('='))[1].strip()
      self._endTime = (lines[2].split('='))[1].strip()
      
      
    # 需要压缩的文件
    self._fileList = []
    with open(self._logFiles, 'rb') as f:
      text = f.read().decode('utf_8_sig')
      lines = text.split('\n')
      for i in range(len(lines)):
        line = lines[i].strip()
        if len(line) > 0:
          self._fileList.append(line)
          
    # 构造方法准备完成
    return
    
  ## 执行主方法, 现在就打包信息文件了,需要rar的一并打包并添加rr3吧.
  def main(self):
    with zipfile.ZipFile(self._zipFpath, 'w') as myZip:
      print('Start to zip files,please wait!')
      for srcname in self._fileList:
        arcname = srcname.replace(self._rootDir, self._zipRoot)
        myZip.write(srcname, arcname = arcname)
      myZip.comment = (self._rootDir+'\nFrom:'+self._startTime+'\nTo:'+self._endTime).encode('utf8')
    return
    
    
## 开始
if __name__ == '__main__':
  zb = ZipBackup()
  zb.main()